package com.knife.blog.web.action;

import java.util.List;

//import com.knife.blog.domain.BlogCircle;
import com.knife.blog.domain.BlogCircleMember;
import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.BlogUser;
import com.knife.blog.domain.PhotoDir;
import com.knife.blog.logic.BlogCircleDirService;
import com.knife.blog.logic.BlogCircleMemberService;
import com.knife.blog.logic.BlogCircleService;
import com.knife.blog.logic.BlogNewsDocService;
import com.knife.blog.logic.impl.BlogCircleDirServiceImpl;
import com.knife.blog.logic.impl.BlogCircleMemberServiceImpl;
import com.knife.blog.logic.impl.BlogCircleServiceImpl;
import com.knife.blog.logic.impl.BlogCoreServiceImpl;
import com.knife.blog.util.BlogCommUtil;
import com.knife.util.CommUtil;
import com.knife.util.TagUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogCircleAction extends BaseCrudAction {
	private BlogCircleService blogCircleService = BlogCircleServiceImpl.getInstance();
	private BlogCircleDirService circleDirService = BlogCircleDirServiceImpl.getInstance();
	private BlogCircleMemberService circleMemberService = BlogCircleMemberServiceImpl.getInstance();
	private BlogNewsDocService blogDocService = BlogCoreServiceImpl.getInstance();
	
	public Object doBefore(WebForm form, Module module) {
		if(this.getCurrentUser(form)!=null){
			blog = userService.getUserBlog(this.getCurrentUser(form).getUserName());
		}
		return super.doBefore(form, module);
	}

	public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
		IPageList list = blogCircleService.queryAllUserCircle(blog.getUsername(),currentPage,pageSize);
		BlogCircleMember member = circleMemberService.getCircleMemberByUserName(blog.getUsername());
		if(list.getResult()==null){
			form.addResult("msg", "对不起，你还没有建立或参与圈子！");
			return null;
		}
		form.addResult("member", member);
		return list;
	}
	public Page doManageQuery(WebForm form, Module module) {
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		//String dirId = CommUtil.null2String(form.get("cid"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		String query = CommUtil.null2String(form.get("queryTitle"));
		IPageList pageList = blogCircleService.queryBlogCircle(null,null,null,null,currentPage,pageSize,query);
		// System.out.println("查询到的文章数目是："+pageList.getRowCount());
		CommUtil.saveIPageList2WebForm(pageList, form);
		return module.findPage("manageList");
	}
	public Page doManageList(WebForm form, Module module) {
		
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		IPageList pageList = blogCircleService.queryBlogCircle(null,null,null,null,currentPage,pageSize);
		CommUtil.saveIPageList2WebForm(pageList, form);
		return module.findPage("manageList");
	}
	
	
	/*public Page doChoiceCircle(WebForm form, Module module){
		String cid = (String)form.get("cid");
		List list = blogCircleService.queryAllUserCircle(blog.getUserName()).getResult();
		form.addResult("list", list);
		return new Page("xmlChoice","/xmlChoice.xml","template");
	}*/
	
	public Page doCircleReg(WebForm form,Module module){
		/*List list = circleDirService.queryBlogCircleDir().getResult();
		String test = TagUtil.options(list,"cid","dirName","0000");
		System.out.println(test);*/
		return module.findPage("edit");
	}
	
	public Page doQueryArticle(WebForm form, Module module){
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		String circleSn = (String)form.get("circlesn");
		IPageList commentPList = blogDocService.queryCircleNewsDoc(circleSn,currentPage,pageSize);
		if(commentPList!=null){
			//List list = pList.getResult();
			form.addResult("commentList",commentPList.getResult());
			form.addResult("pages", new Integer(commentPList.getPages()));
			form.addResult("rows", new Integer(commentPList.getRowCount()));
			form.addResult("page", new Integer(commentPList.getCurrentPage()));
			form.addResult("gotoPageHTML", BlogCommUtil.showSimplePageHtml(commentPList
					.getCurrentPage(), commentPList.getPages()));
		}
		return crudPage(form,module,"list");
	}
	
	public IPageList queryArticle(String circleSn){
		return null;
	}
	
	public Page doSaveCircle(WebForm form,Module module){
		BlogInfo circle = (BlogInfo)form2Obj(form);
		if(blogCircleService.getBlogCircle("domain", circle.getDomain())!=null){
			form.addResult("msg", "域名已被占用！");
			return module.findPage("edit");
		}
		circle.setUpdated(Boolean.TRUE);
		circle.setFirtpagenum(10);
		circle.setListnum(10);
		circle.setManagelistnum(10);
		circle.setNewmessagenum(10);
		circle.setTopreadnum(10);
		circle.setTopreviewnum(10);
		circle.setStatus(new Integer(0));
		circle.setInputtime(new java.util.Date());
		circle.setInputuser((String)form.get("inputuser"));
		circle.setTemplate("default");
		circle.setType("circleBlog");
		if(blogCircleService.addBlogCircle(circle)){
			//form.addResult("msg","申请已提交，请等待管理员审核！");
			IActiveUser user = this.getCurrentUser(form);
			BlogUser blogUser = userService.getBlogUser(user.getUserName());
			BlogCircleMember member = circleMemberService.blogUser2BlogCircleMember(blogUser);
			member.setStatus(0);
			//member.setNickName((String)form.get("nickName"));
			circleMemberService.save(member);
			userService.settingBlogInfo(blog);
			form.addResult("msg","申请成功！");
			return super.doQuery(form, module,user);
		}else{
			form.addResult("msg","申请失败！");
			return module.findPage("edit");
		}
	}
	
    @Override
	public Page doEdit(WebForm form, Module module, IActiveUser user) {
		Page forward = null;
		Object obj = form2Obj(form);
		if (obj != null) {
			CommUtil.Obj2Map(obj, form.getTextElement());
			forward = crudPage(form,module,"edit");
		} else {
			form.addResult("msg", "找不到数据！");
			forward = doQuery(form, module, user);
		}
		return forward;
	}
	public Page doManageEdit(WebForm form, Module module){
	    String cid=(String)form.get("cid");
	    BlogInfo blogcircle = blogCircleService.getBlogCircleByCid(cid);
        form.addPo(blogcircle);
        return module.findPage("manageEdit");
}
	public Page doManageUpdate(WebForm form, Module module){
		boolean ret = blogCircleService.updateBlogCircle((BlogInfo) form2Obj(form));
		form.addResult("msg", ret ? "修改成功!" : "修改失败!");
		return doManageList(form, module);
}
    @Override
	public Page doDel(WebForm form, Module module, IActiveUser user) {
		boolean ret = blogCircleService.delBlogCircle((String) form.get("cid"));
		form.addResult("msg", ret ? "删除成功!" : "删除失败!");
		return super.doQuery(form, module, user);
	}
	public Page doManageDel(WebForm form, Module module) {
		boolean ret = blogCircleService.delBlogCircleByID((String) form.get("cid"));
		form.addResult("msg", ret ? "删除成功!" : "删除失败!");
		return doManageList(form, module);
	}
	public Page doDelBatch(WebForm form, Module module, IActiveUser user) {
		int ret = blogCircleService.batchDelBlogCircleByID((String[]) form.get("cid"));
		form.addResult("msg", ret==0 ? "删除"+ret+"项成功!" : "删除失败!");
		return super.doQuery(form, module, user);
	}
	public Page doManageDelBatch(WebForm form, Module module) {
		int ret = blogCircleService.batchDelBlogCircle((String[]) form.get("cid"));
		form.addResult("msg", ret==0 ? "删除"+ret+"项成功!" : "删除失败!");
		return doManageList(form, module);
	}
	
	public Page doJoin(WebForm form, Module module){
		BlogCircleMember member = circleMemberService.getCircleMember((String)form.get("cid"));
		member.setStatus(new Integer(1));
		boolean ret = circleMemberService.update(member);
		form.addResult("msg", ret ? member.getInputuser()+"成功加入圈子!" : "审核失败!");
		return super.doQuery(form, module, this.getCurrentUser(form));
	}
	
	public Page doShowApply(WebForm form, Module module){
		IActiveUser user=getCurrentUser(form);
		if(user == null){
			form.addResult("msg", "请先登陆！");
			return module.findPage("login");
		}
		form.addResult("cid", (String)form.get("cid"));
		return module.findPage("apply");
	}
	
	public Page doApply(WebForm form, Module module){
		String username = (String)form.get("username");
		BlogUser user = userService.getBlogUser(username);
		if(user==null){
			form.addResult("msg", "用户不存在！请确认用户名填写无误！");
			return module.findPage("apply");
		}
		BlogCircleMember member = circleMemberService.getCircleMember(username);
		if(member!=null){
			form.addResult("msg", "该成员已经存在，可能你已经是该圈子成员！");
		}
		member = circleMemberService.blogUser2BlogCircleMember(user);
		member.setNickname((String)form.get("nickname"));
		member.setSn((String)form.get("cid"));
		member.setStatus(0);
		boolean ret = circleMemberService.save(member);
		form.addResult("msg", ret?"申请已提交，请等待管理员审核！":"提交失败！");
		return module.findPage("apply");
	}
	
	public Page doQuit(WebForm form,Module module){
		boolean ret = blogCircleService.delBlogCircleMember((String)form.get("cid"));
		IActiveUser user = this.getCurrentUser(form);
		if(ret){
			form.addResult("msg","你已经成功退出该圈子！");
		}else{
			form.addResult("msg","退出圈子失败！");
		}
		return super.doQuery(form,module,user);
	}
	
	public List queryTopCircleDir(){
		List list;
		return null;
		
	}
	
	public List queryChildCircleDir(){
		List list;
		return null;
	}
	
	public IPageList doMember(WebForm form){
		BlogInfo circle = (BlogInfo)form2Obj(form);
		return blogCircleService.queryBlogCircleMember(circle,0,-1);
	}
	
	public Page doMemberManage(WebForm form,Module module){
		Page forward = null;
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		String cid = (String)form.get("cid");
		IPageList pList =  circleMemberService.queryCircleMemberByCircleCid(cid,currentPage, pageSize);
		if (pList != null) {
			form.addResult("list", pList.getResult());
			form.addResult("pages", new Integer(pList.getPages()));
			form.addResult("rows", new Integer(pList.getRowCount()));
			form.addResult("page", new Integer(pList.getCurrentPage()));
			form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList
					.getCurrentPage(), pList.getPages()));
		}
		return crudPage(form,module,"memberList");
	}
	
	public Page doVisitCount(WebForm form, Module module){
		String cid = CommUtil.null2String(form.get("cid"));
		BlogInfo circle = blogCircleService.getBlogCircleByCid(cid);
		circle.setReadtimes(new Integer(circle.getReadtimes().intValue())!=null?circle.getReadtimes().intValue()+1:+1);
		blogCircleService.updateBlogCircle(circle);
		return null;
	}
	
	public Object form2Obj(WebForm form) {
		String cid = CommUtil.null2String(form.get("cid"));
		BlogInfo obj = blogCircleService.getBlogCircleByCid(cid);
		if (obj == null)
			obj = new BlogInfo();
		form.toPo(obj);
		return obj;
	}
	
	/*public Object form2Obj(WebForm form) {
		String cid = CommUtil.null2String(form.get("cid"));
		BlogCircle obj = blogCircleService.getBlogCircleByCid(cid);
		if (obj == null)
			obj = new BlogCircle();
		form.toPo(obj);
		return obj;
	}*/

}
